const  userService = require('../../services/admin/userService')

const JWT = require('../../utils/JWT')

const userController = {
    // 用户登录接口逻辑
    login: async (req,res,next) => {
        const result = await userService.login(req.body)

        if (result.length === 0) {
            res.send({
                code: "-1",
                error: "用户名或密码错误"
            })
        }else {
            // 生成token
            const token = JWT.generate({
                _id: result[0]._id,
                username: result[0].username,
            }, "1d")

            // 设置到请求头
            res.header('Authorization', token)

            res.send({
                code: "200",
                ActionType: "OK",
                // 返回数据
                data: {
                    username: result[0].username,
                    gender: result[0].gender || 0, // 性别 0 代表保密，1代表男性， 2代表女性
                    introduction: result[0].introduction || 'fcwcsadcds', // 用户简介
                    avatar: 'http://localhost:3000' + result[0].avatar || '', // 用户头像
                    role: result[0].role, // 用户角色 2代表普通用户，1代表管理员
                }
            })
        }
       


    },

    // 修改个人信息逻辑
    upload: async (req, res, next) => {
        
        // 调用数据库操作模块 
        const token  = req.headers.authorization.split(' ')[1] 
        const playload = JWT.verify(token)
        console.log(playload._id);

         await userService.upload({
            _id: playload._id,
            username: req.body.username,
            gender: req.body.gender,
            introduction: req.body.introduction,
            avatar:  req.file ? `/avatarUploads/${req.file.filename}` : '',
            password: req.body.password
        })

      if (req.file) {
        res.send({
            ActionType: "OK",
            message: '修改成功', 
            data: {
                username:  req.body.username,
                avatar:  `http://localhost:3000/avatarUploads/${req.file.filename}`,
                gender: req.body.gender,
                introduction:req.body.introduction
            }
        })

      }else {
        res.send({
            ActionType: "OK",
            message: '修改成功', 
            data: {
                username:  req.body.username,
                gender: req.body.gender,
                introduction:req.body.introduction
            }
        })

      }

    },

    // 添加用户逻辑
    add: async (req, res, next) => {
        
        //  提取token
         const token  = req.headers.authorization.split(' ')[1] 
         // 验证token
         const playload = JWT.verify(token)

        //  调用数据库操作模块 
          await userService.add({
             _id: playload._id,
             username: req.body.username,
             gender: req.body.gender,
             introduction: req.body.introduction,
             avatar:  req.file ? `/avatarUploads/${req.file.filename}` : '',
             password: req.body.password,
             role: req.body.role
         })
        // // 返回提示信息
        console.log(req);
        res.send({
            ActionType: "OK",
            message: '添加成功', 
            status: 200,

        })
 
    },

    // 获取用户列表逻辑
    list: async (req, res, next) => {
         //  调用数据库操作模块
        const result = await  userService.list()

        // 返回数据
        res.send({
            ActionType: "OK",
            data: result
        })

    },

    // 用户删除
    delete: async (req, res, next) => {
        // 获取用户id
        const { id } = req.body
        // 调用数据库操作模块
        await userService.delete(id)
        // 返回数据
        res.send({
            ActionType: "OK",
            status: 200
        })
    },

    // 用户编辑
    editGet: async (req, res, next) => {
        // 获取用户id
        const id = req.query.id
        // // 调用数据库操作模块
        const result = await userService.editGet(id)
        
       
        // 返回数据
        res.send({
            ActionType: "OK",
            status: 200,
            data: result
        })
    },

    // 数据提交
    editSumbit: async (req, res, next) => {
        // 获取用户id和数据
        const data = req.body
        // 调用数据库操作模块
        await userService.editSumbit(data)
        // 返回数据
        res.send({
            ActionType: "OK",
            status: 200,
            message: '编辑成功'
        })
    }
}

module.exports = userController